﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Data;
using QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc;


namespace QuanLyDuocPham.Controllers.QuanLyHeThong.NhapXuat
{
    class DDH_XUAT_CT_Control : IDisposable
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
            
        }
        //----------------------------------------------------------------------------------
        //------------------------------------TEMP2-----------------------------------------
        public void clearTemp2()
        {
            foreach (var i in db.TEMP2_Get_List())
            {
                db.TEMP2_Delete(i.MA_DUOCPHAM);
            }
        }
        private void addTemp2(ListView ls, ComboBox cmbDuocPham, TextBox txtGia_Ban, TextBox txtSo_Luong)
        {
            var cmbDuocPhamSelected = (DUOCPHAM_Get_ListResult)cmbDuocPham.SelectedItem;
            db.TEMP2_Insert((ls.Items.Count + 1), cmbDuocPhamSelected.MA_DUOCPHAM, cmbDuocPhamSelected.TEN_DUOCPHAM, int.Parse(txtGia_Ban.Text), Int32.Parse(txtSo_Luong.Text), (int.Parse(txtGia_Ban.Text) * Int32.Parse(txtSo_Luong.Text)));
        }
        private void deleteTemp2(string ID1_Delete, ListView ls)
        {
            db.TEMP2_Delete(ID1_Delete);
            int stt = 1;
            foreach (var i in db.TEMP2_Get_List())
            {
                if (i.STT != stt)
                    i.STT = stt;
                stt++;
            }

        }

        private void updateTemp2(ListView ls, ComboBox cmbDuocPham, TextBox txtGia_Ban, TextBox txtSo_Luong)
        {
            var lsSelected = (TEMP2_Get_ListResult)ls.SelectedItem;
            var cmbDuocPhamSelected = (DUOCPHAM_Get_ListResult)cmbDuocPham.SelectedItem;
            db.TEMP2_Update(lsSelected.STT, cmbDuocPhamSelected.MA_DUOCPHAM, lsSelected.MA_DUOCPHAM, cmbDuocPhamSelected.TEN_DUOCPHAM, int.Parse(txtGia_Ban.Text), int.Parse(txtSo_Luong.Text), (int.Parse(txtGia_Ban.Text) * int.Parse(txtSo_Luong.Text)));
        }
        public void TongTien(Label lbTongTien)
        {
            decimal tong = 0;
            if (db.TEMP2_Get_List().Count() == 0)
            {
                lbTongTien.Content = "Tổng Thành Tiền = 0";
            }
            else
            {

                //var list = db.TEMP2_Get_List();
                //foreach (var i in list)
                //{
                //    tong = tong + decimal.Parse(i.THANH_TIEN.ToString());

                //}
                tong = decimal.Parse(db.TEMP2_Get_List().Sum(w => w.THANH_TIEN).ToString());
                lbTongTien.Content = "Tổng Thành Tiền = " + tong;
            }
        }
        public decimal TongTien(string ma_xuat)
        {
            decimal tong = 0;
            if (db.PHIEUXUAT_CT_GetByMaPhieuxuat(ma_xuat).Count() == 0)
            {
                tong = 0;
            }
            else
            {
                tong = decimal.Parse(db.PHIEUXUAT_CT_GetByMaPhieuxuat(ma_xuat).Sum(w => (w.SO_LUONG * w.GIA_BAN)).ToString());

            }
            return tong;
        }
        public void loadDataTemp_Add(ListView List)
        {
            List.ItemsSource = null;
            var list = db.TEMP2_Get_List();
            List.ItemsSource = list;
        }

        GIA_Control giactrl = new GIA_Control();
        public void loadDataTemp2_Edit(string ma_nhap, ListView List)
        {
            var list = db.PHIEUXUAT_CT_GetByMaPhieuxuat(ma_nhap);
            int stt = 1;
            foreach (var i in list)
            {
                db.TEMP2_Insert(stt, i.MA_DUOCPHAM, db.DUOCPHAM_GetByMaDuocpham(i.MA_DUOCPHAM).Single().TEN_DUOCPHAM, int.Parse(i.GIA_BAN.ToString()), i.SO_LUONG, (int.Parse(i.GIA_BAN.ToString()) * i.SO_LUONG));
                stt++;
            }
            List.ItemsSource = db.TEMP2_Get_List();
        }


        public void addandloadDataTemp2()
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                int stt = 1;
                foreach (DataRow i in ds.Tables[0].Rows)
                {
                    db.TEMP2_Insert(stt, i.ItemArray[0].ToString(), db.DUOCPHAM_GetByMaDuocpham(i.ItemArray[0].ToString()).Single().TEN_DUOCPHAM, int.Parse(i.ItemArray[5].ToString()), int.Parse(i.ItemArray[4].ToString()), (int.Parse(i.ItemArray[5].ToString()) * int.Parse(i.ItemArray[4].ToString())));
                    stt++;
                }
            }

        }

        //-------------------------Xử lý trên bảng tạm---------------------
        DataSet ds = new DataSet();

        public void TaoBangTam()
        {

            var names = db.Mapping.MappingSource.GetModel(typeof(DuocPhamDataContext)).GetMetaType(typeof(PHIEUXUAT_CT)).DataMembers;
            foreach (var i in names)
            {
                ds.Tables[0].Columns.Add(i.MappedName, i.Type);
            }
        }
        public void XuLyBangTam(string thaotac, ListView lv1, string ma_nhap, ComboBox cmbDuocPham, TextBox txtTL_HoaHong, TextBox txtSo_Luong, TextBox txtGia_Ban, TextBox txtTL_ChietKhau, RichTextBox txtDien_Giai)
        {

            switch (thaotac)
            {
                case "add":
                    DataRow dr;
                    var cmb2Selected = (DUOCPHAM_Get_ListResult)cmbDuocPham.SelectedItem;
                    dr = ds.Tables[0].NewRow();
                    dr[0] = cmb2Selected.MA_DUOCPHAM;
                    dr[1] = ma_nhap;
                    dr[2] = Int32.Parse(txtSo_Luong.Text);
                    dr[3] = decimal.Parse(txtGia_Ban.Text);
                    dr[4] = double.Parse(txtTL_ChietKhau.Text);
                    dr[5] = txtTL_HoaHong.Text;            
                    dr[6] = txtDien_Giai.Document.Blocks.ToString();
                    ds.Tables[0].Rows.Add(dr);
                    addTemp2(lv1, cmbDuocPham, txtGia_Ban, txtSo_Luong);

                    break;


                case "update":
                    var cmbSelected = (DUOCPHAM_Get_ListResult)cmbDuocPham.SelectedItem;
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[0] = cmbSelected.MA_DUOCPHAM;
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[1] = ma_nhap;
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[2] = Int32.Parse(txtSo_Luong.Text);
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[3] = decimal.Parse(txtGia_Ban.Text);
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[4] = double.Parse(txtTL_ChietKhau.Text);
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[5] = txtTL_HoaHong.Text;                   
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[6] = txtDien_Giai.Document.Blocks.ToString();
                    
                    updateTemp2(lv1, cmbDuocPham, txtGia_Ban, txtSo_Luong);

                    break;


                case "delete":
                    var lvselect = (TEMP2_Get_ListResult)lv1.SelectedItem;
                    ds.Tables[0].Rows.RemoveAt(lv1.SelectedIndex);
                    deleteTemp2(lvselect.MA_DUOCPHAM, lv1);

                    break;
            }

        }
        public void XoaBangTam()
        {
            ds.Dispose();
        }

        public void DDHXUAT_CTToTam(string ma_xuat)
        {
            if (db.PHIEUXUAT_CT_GetByMaPhieuxuat(ma_xuat).Count() > 0)
            {
                ds.Dispose();
                foreach (var i in db.PHIEUXUAT_CT_GetByMaPhieuxuat(ma_xuat))
                {
                    DataRow dr;

                    dr = ds.Tables[0].NewRow();
                    dr[0] = ma_xuat;
                    dr[1] = i.MA_DUOCPHAM;
                    dr[2] = i.MA_KHO;
                    dr[3] = i.MA_LO;
                    dr[4] = i.SO_LUONG;
                    dr[5] = i.GIA_BAN;
                    dr[6] = i.TL_CHIETKHAU;
                    dr[7] = i.TL_HOAHONG;
                    
                    ds.Tables[0].Rows.Add(dr);
                }
            }
        }
        DUOCPHAM_Control ctrl1 = new DUOCPHAM_Control();
        public void TamToView(ListView lv1, ComboBox cmbDuocPham, TextBox txtTL_HoaHong, TextBox txtSo_Luong, TextBox txtGia_Ban, TextBox txtTL_ChietKhau, RichTextBox txtDien_Giai)
        {

            var sl = ds.Tables[0].Rows[lv1.SelectedIndex];

            txtSo_Luong.Text = sl.ItemArray[2].ToString();
            txtTL_HoaHong.Text = sl.ItemArray[5].ToString();
            txtDien_Giai.AppendText( sl.ItemArray[6].ToString());
            txtTL_ChietKhau.Text = sl.ItemArray[4].ToString();
            txtGia_Ban.Text = sl.ItemArray[3].ToString();

            ctrl1.loadComboBox(cmbDuocPham);
            int t = 0;
            foreach (var i in cmbDuocPham.Items)
            {
                var temp = (DUOCPHAM_Get_ListResult)i;
                if (temp.MA_DUOCPHAM == sl.ItemArray[0].ToString())
                {
                    break;
                }
                t++;
            }
            cmbDuocPham.SelectedIndex = t;
            
            

        }
        //--------------------------------------------------------------
        public void TamToXUATCT()
        {
            foreach (DataRow i in ds.Tables[0].Rows)
            {
                db.PHIEUXUAT_CT_Insert(i.ItemArray[0].ToString(), i.ItemArray[1].ToString(), i.ItemArray[2].ToString(), i.ItemArray[3].ToString(), int.Parse(i.ItemArray[4].ToString()), int.Parse(i.ItemArray[5].ToString()), double.Parse(i.ItemArray[6].ToString()), double.Parse(i.ItemArray[7].ToString()));
            }
        }

        public void deleteAllXUATCT(string ma_xuat, string ma_kho)
        {
            if (db.PHIEUXUAT_CT_GetByMaPhieuxuat(ma_xuat).Count() > 0)
            {
                foreach (var i in db.PHIEUXUAT_CT_GetByMaPhieuxuat(ma_xuat))
                {
                    db.PHIEUXUAT_CT_Delete(ma_kho, ma_xuat, i.MA_DUOCPHAM,i.MA_LO);
                }
            }
        }
    }
}
